package com.ruoyi.caigou.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.caigou.domain.dto.BuyOrdDTO;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.annotation.InnerAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.caigou.domain.BuyOrder;
import com.ruoyi.caigou.service.IBuyOrderService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 采购订单Controller
 * 
 * @author lvql
 * @date 2024-11-02
 */
@RestController
@RequestMapping("/buyOrder")
public class BuyOrderController extends BaseController
{
    @Autowired
    private IBuyOrderService buyOrderService;

    /**
     * 查询采购订单列表
     */
    @RequiresPermissions("caigou:buyOrder:list")
    @GetMapping("/list")
    public TableDataInfo list(BuyOrder buyOrder)
    {
        startPage();
        List<BuyOrder> list = buyOrderService.selectBuyOrderList(buyOrder);
        return getDataTable(list);
    }

    @RequiresPermissions("caigou:buyOrder:list")
    @GetMapping("/noReconList")
    public TableDataInfo noReconList(BuyOrder buyOrder)
    {
        startPage();
        List<BuyOrder> list = buyOrderService.noReconList(buyOrder);
        return getDataTable(list);
    }

    /**
     * 导出采购订单列表
     */
    @RequiresPermissions("caigou:buyOrder:export")
    @Log(title = "采购订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuyOrder buyOrder)
    {
        List<BuyOrder> list = buyOrderService.selectBuyOrderList(buyOrder);
        ExcelUtil<BuyOrder> util = new ExcelUtil<BuyOrder>(BuyOrder.class);
        util.exportExcel(response, list, "采购订单数据");
    }

    /**
     * 获取采购订单详细信息
     */
    @RequiresPermissions("caigou:buyOrder:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(buyOrderService.selectBuyOrderById(id));
    }
    /**
     * 获取当天采购订单笔数及金额
     */
    @RequiresPermissions("caigou:buyOrder:query")
    @GetMapping(value = "getBuyOrderCountAndMoney")
    public AjaxResult getBuyOrderCountAndMoney()
    {
        return success(buyOrderService.getBuyOrderCountAndMoney());
    }
    /**
     * 获取采购订单详细信息
     */
    @InnerAuth
    @GetMapping(value = "/getByCode/{code}")
    public R<BuyOrder> getInnerInfo(@PathVariable("code") String code)
    {
        BuyOrder buyOrder = new BuyOrder();
        buyOrder.setCode(code);
        List<BuyOrder> buyOrders = buyOrderService.selectBuyOrderList(buyOrder);
        if (buyOrders != null && buyOrders.size() > 0) {
            return R.ok(buyOrders.get(0));
        }
        return R.fail("没有该单据的采购订单");
    }

    /**
     * 新增采购订单
     */
    @RequiresPermissions("caigou:buyOrder:add")
    @Log(title = "采购订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuyOrder buyOrder)
    {
        return toAjax(buyOrderService.insertBuyOrder(buyOrder));
    }

    @RequiresPermissions("caigou:buyOrder:add")
    @Log(title = "采购订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addPlus(@RequestBody BuyOrdDTO buyOrdDTO)
    {
        logger.info("BuyOrderController  addPlus params {}", buyOrdDTO);
        return toAjax(buyOrderService.insertBuyOrderPlus(buyOrdDTO));
    }

    /**
     * 修改采购订单
     */
//    @RequiresPermissions("caigou:buyOrder:edit")
    @InnerAuth
    @Log(title = "采购订单", businessType = BusinessType.UPDATE)
    @PutMapping("/innerEdit")
    public R<Boolean> edit(@RequestBody BuyOrder buyOrder)
    {
        return R.ok(buyOrderService.updateBuyOrder(buyOrder)>0);
    }

    @InnerAuth
    @Log(title = "采购订单", businessType = BusinessType.UPDATE)
    @PutMapping("/innerEditByCode")
    public R<Boolean> innerEditByCode(@RequestBody BuyOrder buyOrder)
    {
        return R.ok(buyOrderService.updateBuyOrderByCode(buyOrder)>0);
    }

    @RequiresPermissions("caigou:buyOrder:edit")
    @Log(title = "采购订单", businessType = BusinessType.UPDATE)
    @PutMapping("/upd")
    public AjaxResult editPlus(@RequestBody BuyOrdDTO buyOrdDTO)
    {
        return toAjax(buyOrderService.updateBuyOrderPlus(buyOrdDTO));
    }

    @RequiresPermissions("caigou:buyOrder:edit")
    @Log(title = "审核采购订单", businessType = BusinessType.UPDATE)
    @PostMapping("/approval")
    public AjaxResult approval(@RequestBody BuyOrder buyOrder)
    {
        return toAjax(buyOrderService.approvalBuyOrder(buyOrder));
    }

    /**
     * 删除采购订单
     */
    @RequiresPermissions("caigou:buyOrder:remove")
    @Log(title = "采购订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
//        return toAjax(buyOrderService.deleteBuyOrderByIds(ids));
        return toAjax(buyOrderService.deleteBuyOrderByIdsPlus(ids));
    }
}
